<template>
  <el-container class="Group-lessons">
    <el-header></el-header>
    <el-main>
      <div class="Group-lessons-main">
        <div class="Group-lessons-main-1">
          <div class="add-title">
            <span>预约记录</span>
            <el-button type="primary" :icon="ArrowLeft" class="back-style"
              ><router-link to="/mainView/teamManagement/ScheduleView/ListView"
                >返回</router-link
              ></el-button
            >
          </div>
        </div>
        <el-row class="Group-lessons-main-1">
          <img
            src="../../styles/bg/u5085.png"
            style="width: 100px; height: 100px"
          />
          <div class="text-show">
            <span>{{ state.tableData.courseName }}</span>
            <span
              >{{ state.tableData.courseTime }} 教练
              {{ state.tableData.classTime }}分钟</span
            >
            <span
              >已预约:{{ state.tableData.appointmentNumber }} 已签到:{{
                state.tableData.signedNumber
              }}
              已取消:{{ state.tableData.cancelNumber }} 候补中:{{
                state.tableData.candidateNumber
              }}</span
            >
            <div class="text-show1">
              <el-button type="danger" round class="button-min">编辑</el-button>
              <el-button type="info" round class="button-min"
                >删除课时</el-button
              >
            </div>
          </div>
        </el-row>
        <el-row class="List-title"> 预约记录 </el-row>
        <el-row>
          <el-table
            :data="state.tableData.appointmentRecordVoList"
            border
            style="width: 100%"
          >
            <el-table-column prop="userName" label="姓名" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="appointmentNumber" label="人数" />
            <el-table-column prop="classTime" label="上课时间" />
            <el-table-column label="状态">
              <template #default="scope">
                {{ scope.row.signedStatus == 1 ? "已签到" : "未签到" }}
              </template>
            </el-table-column>
            <el-table-column
              prop="appointmentTime"
              label="预约时间"
              width="180"
            />
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button
                  link
                  type="primary"
                  size="small"
                  @click="open1(scope.row.appointmentRecordId)"
                  >签到</el-button
                >
                <el-button
                  link
                  type="primary"
                  size="small"
                  @click="open(scope.row.appointmentRecordId)"
                  >取消预约</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
import { ArrowLeft } from "@element-plus/icons-vue";
import { reactive, ref, nextTick, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { useRoute } from "vue-router";
import {
  findAppointmentRecord,
  cancelCoachSign,
  cancelAppointmentRecord,
} from "../../http/api";
// 编辑时路由传过来的id
let zid = useRoute().query.zid;
var state = reactive({ tableData: [] });
// 取消预约
const open = (id) => {
  ElMessageBox.confirm("确定要为会员取消预约吗？?", "系统提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      cancelAppointmentRecord({ appointmentRecordId: id }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            type: "success",
            message: "取消成功！",
          });
          findData();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "放弃取消！",
      });
    });
};
// 签到
const open1 = (id) => {
  ElMessageBox.confirm("确定要为会员签到吗？", "系统提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      state.tableData.appointmentRecordVoList.forEach((el) => {
        if (el.appointmentRecordId == id) {
          if (el.signedStatus == 1) {
            ElMessage({
              type: "warning",
              message: "请勿重复签到！",
            });
          } else {
            cancelCoachSign({ appointmentRecordId: id }).then((res) => {
              if (res.code == 0) {
                ElMessage({
                  type: "success",
                  message: "签到成功！",
                });
                findData();
              }
            });
          }
        }
      });
      // ElMessage({
      //   type: "success",
      //   message: "签到成功！",
      // });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "放弃签到！",
      });
    });
};

onMounted(() => {
  findData();
});
const findData = () => {
  findAppointmentRecord({ courseId: zid }).then((res) => {
    state.tableData = res.data;
    state.tableData.appointmentRecordVoList.forEach((el) => {
      console.log(el);
      el.appointmentTime = new Date(el.appointmentTime).toLocaleString();
    });
  });
};
</script>

<style scoped>
.Group-lessons {
  background-color: #f2f2f2;
  height: 100vh;
}
.Group-lessons-main {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 10px;
}
.Group-lessons-main-1 {
  border-radius: 15px;
  border: 1px solid #e4e4e4;
  overflow: hidden;
  margin-top: 10px;
  padding: 10px;
}
.add-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f3f3f3;
  padding: 5px 10px;
}
.text-show > span {
  display: block;
  font-size: 12px;
  font-weight: 400;
  padding: 5px;
}
.text-show1 {
  padding: 0 10px;
}
.button-min {
  width: 78px !important;
  height: 28px !important;
}
.List-title {
  padding: 10px 5px;
  background-color: #f3f3f3;
  margin-top: 10px;
}
.back-style a {
  color: white;
  text-decoration: none;
}
</style>
